//
//  ViewController.m
//  03-UIGestureRecognizer
//
//  Created by 王道林 on 16/7/18.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    // 手势识别器
   //   UIGestureRecognizer   所有手势的基类
    //分为7种:
    //1. 轻拍
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    //点击几次触发方法
    tap.numberOfTapsRequired = 1;
    //几个手指触发(按option  -  alt)
    tap.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:tap];
    
    //2. 长按 longpress
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressACtion:)];
    // 长按几秒后开始 ,默认是0.5 秒
    longPress.minimumPressDuration = 3;
    // 默认10 像素, 长按失败前, 最大的移动距离
    longPress.allowableMovement = 150;
    [self.view addGestureRecognizer:longPress];
    
    //3. 清扫
//    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
//    [self.view addGestureRecognizer:swipe];
//   // direction 可以设置多个, 前提多个方向所触发的行为要相同
//    // 如果多个枚举触发的行为不同, 需要创建多个清扫的手势
//    swipe.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
//    
    // 练习: ImageView 添加清扫换图
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 335, 647)];
    // 图片的交互开启
    imageView.userInteractionEnabled = YES;
    imageView.image = [UIImage imageNamed:@"1.png"];
    [self.view addSubview:imageView];
    
    UISwipeGestureRecognizer *swipePicRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeChange:)];
    
    [imageView addGestureRecognizer:swipePicRight];
    
    UISwipeGestureRecognizer *swipePicleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeChangeLeft:)];
    // 按位枚举,  4个选项 -> 1 1 1 1;
    // 左右 与 上下  有冲突
    swipePicleft.direction = UISwipeGestureRecognizerDirectionLeft;
    [imageView addGestureRecognizer:swipePicleft];

    
    //4. 平移
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [imageView addGestureRecognizer:pan];
    
    //5. 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imageView addGestureRecognizer:pinch];
    
    
    //6. 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [imageView addGestureRecognizer:rotation];
    
    //7.屏幕边缘手势
    
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenAction:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    
    
    
}
// 轻拍
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"轻拍");
}

// 长按
- (void)longPressACtion:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    }else if (longPress.state == UIGestureRecognizerStateEnded)
    {
    NSLog(@"长按结束");
    }
}


- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    NSLog(@"清扫");
}

//清扫换图
- (void)swipeChange:(UISwipeGestureRecognizer *)swipe
{
    UIImageView *imageView = (UIImageView *)swipe.view;
        imageView.image = [UIImage imageNamed:@"2.png"];
}


- (void)swipeChangeLeft:(UISwipeGestureRecognizer *)swipe
{
    UIImageView *imageView = (UIImageView *)swipe.view;
    imageView.image = [UIImage imageNamed:@"1.png"];
}

// 平移
- (void)panAction:(UIPanGestureRecognizer *)pan
{
    if (pan.state == UIGestureRecognizerStateBegan) {
        // 平移开始
    } else if (pan.state == UIGestureRecognizerStateChanged)
    {
    // 平移手势, 根据其他View上移动的过程, 得到最终点
   CGPoint point = [pan translationInView:pan.view];
    NSLog(@"%@", NSStringFromCGPoint(point));
    // 根据Point使pan, View移动
    pan.view.transform = CGAffineTransformMakeTranslation(point.x, point.y);
    // pan 移动的点 置空
        [pan setTranslation:point inView:pan.view];
    } else if (pan.state == UIGestureRecognizerStateEnded)
    {
        pan.view.transform = CGAffineTransformIdentity;
    }
}


// 捏合
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    //限制缩放倍数为1
    pinch.scale = 1;
}

// 旋转
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    //旋转弧度设为0
    rotation.rotation = 0;
}

// 屏幕边缘清扫
- (void)screenAction:(UIScreenEdgePanGestureRecognizer *)screenEdge
{
    NSLog(@"屏幕边缘");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
